//
//  HomeEconomicDetailViewController.m
//  cityStar
//
//  Created by joyban on 15-1-20.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "HomeEconomicDetailViewController.h"
#import "HomEconomicsDetailCell.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
NSString *const HomEconomicsDetailCellID = @"homEconomicsDetailCellID";


@interface HomeEconomicDetailViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labeline;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *service_districtLabel;
@property (weak, nonatomic) IBOutlet UILabel *districtLabel;

@property (weak, nonatomic) IBOutlet UIButton *Consumptionbtn;
@property (weak, nonatomic) IBOutlet UITableView *HomeEconomicDeatailTable;
@property (strong, nonatomic) IBOutlet UIView *heardView;
@property (weak, nonatomic) IBOutlet UILabel *fuwulabel;
@property (weak, nonatomic) IBOutlet UILabel *address;

@end

@implementation HomeEconomicDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"家政详情";
    [self addBackButton];
    [self setUpTableViews];
    self.HomeEconomicDeatailTable.tableHeaderView  = self.heardView;
    [self setheardViewData];
    if([self.withchType isEqualToString:@"cityService"])
    {
        [self.Consumptionbtn setTitle:@"消费积分查看联系方式" forState:UIControlStateNormal];
    }else{
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateNormal];
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateHighlighted];

        [self.Consumptionbtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setheardViewData
{
    if ([self.homeModel.is_need intValue] == 1) {
//        self.fuwulabel.text = @"需要服务";
        self.service_districtLabel.hidden = YES;
        self.fuwuaddress.hidden = YES;
        self.downLine.constant = 29;
        NSInteger timestring = [[NSString stringWithFormat:@"%@",self.homeModel.service_time] integerValue];
        switch (timestring) {
            case 1:
                self.priceLabel.text = @"随时可到";
                break;
            case 2:
                self.priceLabel.text = @"仅工作日";
                break;
            case 3:
                self.priceLabel.text = @"仅节假日";
                break;
            case 4:
                self.priceLabel.text = @"晚上10点前";
                break;
            default:
                break;
        }
        self.address.text = [NSString stringWithFormat:@"%@",self.homeModel.address];
    }else{
        self.service_districtLabel.hidden = NO;
        self.fuwuaddress.hidden = NO;
        self.downLine.constant = 9;
        self.priceLabel.text = [NSString stringWithFormat:@"%@元/小时",self.homeModel.salary];
        self.address.text = [NSString stringWithFormat:@"%@",self.homeModel.address];
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 18)];
    label.font = [UIFont systemFontOfSize:15];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",self.address.text];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    if(labelheight /15 > 2)
    {
        self.labeline.constant = 9;
    }else{
        self.labeline.constant = 27;
    }
    
    self.titleLabel.text = self.homeModel.title;
    self.service_districtLabel.text = self.homeModel.service_district_name;
    self.districtLabel.text = self.homeModel.district_name;
    self.addressLabel.text = @"";
    NSInteger serviceString = [[NSString stringWithFormat:@"%@",self.homeModel.service_type] integerValue];
    switch (serviceString) {
        case 1:
            self.typeLabel.text = @"家庭保洁";
            break;
        case 2:
            self.typeLabel.text = @"公司保洁";
        case 3:
            self.typeLabel.text = @"钟点工";
            break;
        default:
            break;
    }
}

-(void)setUpTableViews
{
    [self.HomeEconomicDeatailTable registerNib:[HomEconomicsDetailCell nib] forCellReuseIdentifier:HomEconomicsDetailCellID];
}

#pragma mark - Actions
-(IBAction)getContact:(id)sender
{
    if ([self.withchType isEqualToString:@"cityService"]) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.homeModel._id,[self.homeModel.type intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                if(![self.Consumptionbtn.titleLabel.text isEqualToString:@"消费积分查看联系方式"])
                {
                    NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                    NSString *telStr=[@"tel://" stringByAppendingString:string];
                    NSURL *url=[NSURL URLWithString:telStr];
                    [[UIApplication sharedApplication]openURL:url];
                }else{
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否消耗积分查看联系方式" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
                    [alertView show];
                }
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
}else{
    RIButtonItem *itemCancel = [RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *itemSubMit = [RIButtonItem itemWithLabel:@"确定" action:^{
        NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_PublishInvalid,self.homeModel._id];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"信息作废成功"];
            }else{
                [SVProgressHUD showErrorWithStatus:@"信息作废失败"];
            }
        }];
    }];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否作废该信息" message:nil cancelButtonItem:itemCancel otherButtonItems:itemSubMit, nil];
    [alertView show];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.homeModel._id,[self.homeModel.type intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                
                NSString *title = [NSString stringWithFormat:@"%@:%@",self.homeModel.contact,string];
                [self.Consumptionbtn setTitle:title forState:UIControlStateNormal];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ss = [NSString stringWithFormat:@"%@",self.homeModel.desc];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 25)];
    label.font = [UIFont systemFontOfSize:14];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",ss];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    return [HomEconomicsDetailCell heightOfTableCell] - (90 - labelheight);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomEconomicsDetailCell *homEconomicsDetailCell = [tableView dequeueReusableCellWithIdentifier:HomEconomicsDetailCellID];
    homEconomicsDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    homEconomicsDetailCell.contentLabel.text = self.homeModel.desc;
    return homEconomicsDetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
